//
//  NewFeatureViewController.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/12.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

private let count = 4

class NewFeatureViewController: UICollectionViewController {
    
    init(){
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
      
    }



    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
//        cell.backgroundColor = indexPath.item % 2 == 0 ?UIColor.greenColor() :UIColor.orangeColor()
        cell.index = indexPath.item
        
        if indexPath.item == 3{
            
            cell.startBtn.hidden = false
        }
        
        return cell
    }
    
    


}


class NewFeatureCell :UICollectionViewCell {
    
    var index : Int = 0{
        
        didSet{
            
            if index == 3{
                
                startBtn.hidden = false
                
                startAnimation()
            }else{
                
                startBtn.hidden = true
            }
            
            backImage.image = UIImage(named: "new_feature_\(index + 1)")
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }
    
    //开始动画的效果
    private func startAnimation() {
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startBtn.hidden = false
            self.startBtn.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
                print("OK")
        }
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        contentView.addSubview(backImage)
        contentView.addSubview(startBtn)
        
        backImage.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView.snp_edges)
        }
        startBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(contentView.snp_centerX)
            make.centerY.equalTo(contentView.snp_centerY).offset(180)
            
        }
        
       startBtn.hidden = true
        
        startBtn.addTarget(self, action: "didBtnClick", forControlEvents: .TouchUpInside)
        
    }
    
    //btn的点击方法
    
    func didBtnClick (){
        
        print("开始体验啦...")
        
        NSNotificationCenter.defaultCenter().postNotificationName("tiaozhuan", object: "welcome")
        
    }

    //懒加载背景图
    lazy var backImage :UIImageView = UIImageView()
    
    lazy var startBtn :UIButton = {
       
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        
        btn.setTitle("开始体验", forState: .Normal)
        return btn
    }()
    
    
    
}
